//
//  SDPhotoTagView.m
//  SKinDiary
//
//  Created by pcyang on 16/7/16.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoTagView.h"

@interface SDPhotoTagView()
@property(nonatomic,strong)UILabel* tagNameLabel;
@property(nonatomic,strong)PhotoTagItem* tagItem;
@end


@implementation PhotoTagItem


@end

@implementation SDPhotoTagView
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor:[UIColor colorWithRGB:0xe8ebf0]];
        [self setTitleColor:[UIColor colorWithRGB:0x747f8b] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        self.layer.cornerRadius = 14;
        self.clipsToBounds = YES;
        [self addTarget:self action:@selector(onTasteTagClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImage* delIcon = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"tag_delete.png");
        _deleteImageview  = [[UIImageView alloc] initWithImage:delIcon];
        _deleteImageview.hidden = YES;
        _deleteImageview.top = (kPhotoTagViewHeight - delIcon.size.height)/2;
        [self addSubview:_deleteImageview];
        
        self.height = kPhotoTagViewHeight;
    }
    
    return self;
}

- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

-(void)onTasteTagClicked:(id)sender
{
    _tagItem.inUsed =!_tagItem.inUsed;
    
    if (_delegate && [_delegate respondsToSelector:@selector(tagViewPressed:)]) {
        [_delegate tagViewPressed:self];
    }
    
    if (_deleteImageview.hidden) {
        [self refresh];
    }
}

-(PhotoTagItem*)getTagItem
{
    return _tagItem;
}

-(void)setTagItem:(PhotoTagItem *)tagItem
{
    _tagItem = tagItem;
    
    [self setTitle:tagItem.title forState:UIControlStateNormal];
    [self sizeToFit];
    self.width += kPhotoTagPadding;
    
    if (!_deleteImageview.hidden) {
        self.width += 10;
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
        _deleteImageview.right = self.width - 10;
    } else {
        [self refresh];
    }
}

-(void)refresh
{
    if (_tagItem.inUsed) {
        [self setBackgroundColor:[UIColor colorWithRGB:0x24d9c6]];
        [self setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
    } else {
        [self setBackgroundColor:[UIColor colorWithRGB:0xe8ebf0]];
        [self setTitleColor:[UIColor colorWithRGB:0x747f8b] forState:UIControlStateNormal];
    }
}
@end
